package com.cdream.basic.algorithm.leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cdream
 * @date 5/21/21
 */
public class Question95 {

    public List<TreeNode> generateTrees(int n) {
        if (n == 0) {
            return new ArrayList<TreeNode>();
        }
        return generateTrees(1, n);

    }

    private List<TreeNode> generateTrees(int start, int end) {
        List<TreeNode> res = new ArrayList<>();
        if (start > end) {
            res.add(null);
            return res;
        }
        for (int i = start; i <= end; i++) {
            List<TreeNode> left = generateTrees(start, i - 1);
            List<TreeNode> right = generateTrees(i + 1, end);

            for (int l = 0; l < left.size(); l++) {
                for (int r = 0; r < right.size(); r++) {
                    TreeNode treeNode = new TreeNode(i);
                    treeNode.left = left.get(l);
                    treeNode.right = right.get(r);
                    res.add(treeNode);
                }
            }
        }
        return res;

    }

    public static void main(String[] args) {
        new Question95().generateTrees(3);
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
